package com.vainycos.lotterysearch.utils;

import com.alibaba.fastjson2.JSONObject;
import com.vainycos.lotterysearch.enums.DingMsgTypeEnums;
import com.vainycos.lotterysearch.model.dingmsg.markdown.MarkdownMessage;
import com.vainycos.lotterysearch.model.dingmsg.markdown.Markdown;
import com.vainycos.lotterysearch.model.dingmsg.text.ContentMessage;
import com.vainycos.lotterysearch.model.dingmsg.text.TextMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author: Vainycos
 * @description 钉钉推送工具类
 * @date: 2022/11/14 14:05
 */
@Slf4j
@Async
@Component
public class DingTalkPushUtil {

    @Value("${ding.webhook-addr:}")
    private String webhookAddr;

    /**
     * 按照钉钉API处理内容格式,默认普通文本
     * @param content
     */
    public void pushText(String content) {
        TextMessage message = new TextMessage();
        ContentMessage messageContent = new ContentMessage();
        message.setMsgtype(DingMsgTypeEnums.TEXT.type);
        messageContent.setContent(content);
        message.setText(messageContent);
        push(message);
        log.info("钉钉推送文本内容成功->{}", message);
    }

    /**
     * 按照钉钉API处理内容格式,支持设置消息类型
     * @param content
     */
    public void pushMarkdown(String title, String content) {
        MarkdownMessage message = new MarkdownMessage();
        message.setMsgtype(DingMsgTypeEnums.MARKDOWN.type);
        Markdown markdownMessage = new Markdown();
        markdownMessage.setTitle(title);
        markdownMessage.setText(content);
        message.setMarkdown(markdownMessage);
        push(message);
        log.info("钉钉推送Markdown内容成功->{}", message);
    }

    /**
     * 推送消息
     * @param obj
     */
    private void push(Object obj) {
        try {
            // 自定义钉钉机器人生成链接  access_token钉钉自动生成
            URL url = new URL("https://oapi.dingtalk.com/robot/send?access_token=251fcace3102a965ccf698fc30c06d92a680e9ee4520a3f9e396cd44e8327d66");
            //打开连接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // Post 请求不能使用缓存
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Charset", "UTF-8");
            conn.setRequestProperty("Content-Type", "application/Json; charset=UTF-8");
            conn.connect();
            OutputStream out = conn.getOutputStream();
            String jsonMessage = JSONObject.toJSONString(obj);
            System.out.println(jsonMessage);
            byte[] data = jsonMessage.getBytes();
            // 发送请求参数
            out.write(data);
            // flush输出流的缓冲
            out.flush();
            out.close();
            //开始获取数据
            InputStream in = conn.getInputStream();
            byte[] content = new byte[in.available()];
            in.read(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}